import 'package:redux/redux.dart';
import 'package:meta/meta.dart';

///------------- push state ---------------------
@immutable
class PushServiceState {
  String regId;
  List tags;

  PushServiceState({this.tags, this.regId});
}

///-------------- push action ------------------------
enum PushServiceActions { SET_TAGS, ADD_TAGS, DELETE_TAGS, CLEAN_TAGS, SET_ALIAS, DELETE_ALIAS, SET_REGID }
class PushServiceAction {
  final PushServiceActions type;
  PushServiceAction({this.type});
}
/// 设置标签
// class SetTagsAction extends PushServiceAction {
//   final List tags;

//   SetTagsAction({this.tags}) : super(type: PushServiceActions.SET_TAGS);
// }
/// 设置state RegId
class SetRegIdAction extends PushServiceAction {
  final String regId;

  SetRegIdAction({this.regId}) : super(type: PushServiceActions.SET_REGID);
}

///------------- push mutations -------------------
setRegId(AppState state, SetRegIdAction action) {
  state.pushService.regId = action.regId;
  print('regId:${action.regId}');
}
// setTags(AppState state, SetTagsAction action) {
//   state.pushService.tags = action.tags;
//   print('tags:${action.tags}');
// }

///------------- player state ---------------------
@immutable
class PlayerState {
  Map screenInfo = {};
  PlayerState({this.screenInfo});
}

///-------------- player action ------------------------
enum PlayerActions { UPDATE_SCREENINFO }
class PlayerAction {
  final PlayerActions type;
  PlayerAction({this.type});
}
class UpdateScreenInfo extends PlayerAction {
  final Map screenInfo;
  UpdateScreenInfo({this.screenInfo}) : super(type: PlayerActions.UPDATE_SCREENINFO);
}
///-------------- player mutations -------------------
updateScreenInfo(AppState state, UpdateScreenInfo action) {
  state.player.screenInfo = action.screenInfo;
}

/// 应用程序状态
class AppState {
  PushServiceState pushService;
  PlayerState player;

  AppState({this.pushService, this.player});
}

AppState mainReducer(AppState state, dynamic action) {
  print("state charge :$action ");
  switch (action.type) {
    // if (action.type == PushServiceActions.SET_TAGS) {
      case PushServiceActions.SET_TAGS: {
        // setTags(state, action);
        break;
      }
    // if (action.type == PushServiceActions.SET_REGID) {
      case PushServiceActions.SET_REGID: {
        setRegId(state, action);
        break;
      }
      default: break;
    // }
  }
  if (action is PlayerAction) {
    switch (action.type) {
      case PlayerActions.UPDATE_SCREENINFO: {
        updateScreenInfo(state, action);
        break;
      }
      default: break;
    }
  }

  print("state changed:$state");

  return state;
}